package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringRabbitListener {
    //简单模型
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String msg) {
//        System.out.println("接收到simple.queue的消息：" + msg);
//    }

    // 工作模型
    @RabbitListener(queues = "simple.queue")
    public void listenWorkerQueue1(String msg) throws InterruptedException {
        System.out.println("接收到simple.queue的消息：" + msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkerQueue2(String msg) throws InterruptedException {
        System.err.println("接收到simple.queue的消息：" + msg);
        Thread.sleep(200);
    }

    // 订阅模型:广播模型
    @RabbitListener(queues = "fanout_queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException {
        System.out.println("接收到fanout 1的消息：" + msg);
    }

    @RabbitListener(queues = "fanout_queue2")
    public void listenFanoutQueue2(String msg) throws InterruptedException {
        System.err.println("接收到fanout 2的消息：" + msg);
    }

    // 订阅模型:直连模型
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct_queue1"),
            exchange = @Exchange(name = "direct_exchange",type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String msg) throws InterruptedException {
        System.out.println("接收到direct 1的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct_queue2"),
            exchange = @Exchange(name = "direct_exchange",type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String msg) throws InterruptedException {
        System.err.println("接收到direct 2的消息：" + msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(name = "topic_queue1"),
            exchange = @Exchange(name = "topic_exchange",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg) throws InterruptedException {
        System.out.println("接收到topic 1的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic_queue2"),
            exchange = @Exchange(name = "topic_exchange", type = ExchangeTypes.TOPIC),
            key = "#.net"
    ))
    public void listenTopicQueue2(String msg) throws InterruptedException {
        System.err.println("接收到topic 2的消息：" + msg);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleJsonQueue1(Map<String,Object> msg) throws InterruptedException {
        System.err.println("接收到json map 的消息：" + msg);
    }
}
